<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表页面-MyBlog</title>
    <style>
        .content{
            height: 28px;
        }
    </style>
    <script src="https://cdn.bootcss.com/jquery/2.1.2/jquery.min.js"></script>
    <script>

        $(function(){

            $("a.del").click(function(){

                var delId = $(this).attr("data-id");

                if(confirm("提示：确认删除？")) {

                    $.post('public/ajax/del_info.php', {delId:delId}, function (data) {
                        if (data == 'ok') {
                            window.location.href = "list.php";
                            return;
                        } else {
                            alert('提示：删除有误!');
                            return;
                        }
                    });

                }


            });

        });

    </script>
</head>
<body>

<?php

//连接数据库
include 'inc/conn.php';

//验证用户登陆
include 'inc/checkSesion.php';

//加载页面头部
include 'inc/admin_header.php';


$pageSize = 2; //每页显示条数

//总条数
$sqlCount = "SELECT COUNT(id) FROM blog_info WHERE id IS NOT NULL";
$resCount = mysqli_query($conn, $sqlCount);
if ($rwtCount = mysqli_fetch_array($resCount)) {
    $infoCount = $rwtCount[0]; //总条数
}

$pageCount = ceil($infoCount/$pageSize); //总页数

$page = intval($_GET['page']); //当前页数
if ($page > $pageCount) {
    $page = $pageCount;
} elseif ($page < 1) {
    $page = 1;
}

$limit = ($page - 1) * $pageSize;


//读取文章内容
$sql = "SELECT id, blog_title, add_time FROM blog_info WHERE id IS NOT NULL ORDER BY id DESC LIMIT $limit, $pageSize  ";
$res = mysqli_query($conn, $sql);
while ($rwt = mysqli_fetch_array($res)) {
    $list[] = $rwt;
}
//var_dump($list);
?>




<table width="99%" align="center" border="1">
    <tr style="height: 30px">
        <th width="10%">ID</th>
        <th width="50%">文章标题</th>
        <th width="20%">发布时间</th>
        <th width="20%">操作</th>
    </tr>

    <?php

    foreach ($list as $val) {
        echo "<tr>
               <td class='content'>".$val['id']."</td>
               <td>".$val['blog_title']."</td>
               <td>".date('Y-m-d H:i:s', $val['add_time'])."</td>
               <td>
                    <a href='edit_blog.php?id=".$val['id']."'>编辑</a>
                    <a href='javascript:void(0);' data-id='".$val['id']."' class='del'>删除</a>
               </td>
          </tr>";
    }

    ?>


    <tr>
        <td colspan="4">

            总计：<?=$infoCount?>条，<?=$page.'/'.$pageCount?>

            <a href="?page=1">首页</a>
            <?php

            if ($page > 1) {
                $prevPage = $page - 1;
                echo "<a href='?page=$prevPage'>上一页</a>";
            }

            if ($page < $pageCount) {
                $nextPage = $page + 1;
                echo "<a href='?page=$nextPage'>下一页</a>";
            }
            ?>


            <a href="?page=<?=$pageCount?>">尾页</a>
        </td>
    </tr>


</table>


</body>
</html>